package com.yuexunit.account.action.v2;

import java.util.ArrayList;
import java.util.List;

import com.yuexunit.account.action.v2.model.Account;
import com.yuexunit.account.action.v2.model.AccountAlias;
import com.yuexunit.account.action.v2.model.Session;
import com.yuexunit.account.action.v2.model.VerifyCodeType;
import com.yuexunit.account.entity.AccountEntity;
import com.yuexunit.account.entity.AccountNameEntity;
import com.yuexunit.account.entity.AccountSessionEntity;
import com.yuexunit.account.service.AccountService;
import com.yuexunit.account.service.VerifyCodeService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;


/**
 * @author wangjinshui
 */
@Implements(AccountActionApi.class)
public class AccountActionImpl implements AccountActionApi {
	private AccountService accountService = ComponentFactory.loadClass(AccountService.class);
	private VerifyCodeService verifyCodeService = ComponentFactory.loadClass(VerifyCodeService.class);
	
	
	@Override
	public Long createAccount(String fullName, String idCardNo, String mobile, String email, String password) {
		Long accountId = accountService.createAccount(fullName, idCardNo, mobile, email, password);

		return accountId;
	}

	@Override
	public void sendVerifyCodeBySms(VerifyCodeType type, String mobile, String idCardNo) {
		accountService.sendVerifyCodeBySms(type, mobile, idCardNo);
	}

	@Override
	public boolean checkVerifyCode(VerifyCodeType type, String mobile, String verifyCode) {
		boolean valid = true;
		try {
			verifyCodeService.checkVerifyCode(type, mobile, verifyCode);
		} catch(Exception ignore) {
			valid = false;
		}
		
		return valid;
	}

	@Override
	public void forgetPassword(String idCardNo, String mobile, String verifyCode, String newPassword) {
		accountService.forgetPassword(idCardNo, mobile, verifyCode, newPassword);
	}

	@Override
	public void changeMobileSms(Long accountId, String oldPassword, String newMobile) {
		accountService.changeMobileSms(accountId, oldPassword, newMobile);
	}

	@Override
	public void changeMobile(Long accountId, String oldPassword, String newMobile, String verifyCode) {
		accountService.changeMobile(accountId, oldPassword, newMobile, verifyCode);
	}

	@Override
	public Session login(String accountName, String password, String appKey, Integer appRoleType) {
		Session session = accountService.login(accountName, password, appKey, appRoleType);
		return session;
	}

	@Override
	public Session loginSso(Long accountId, String appKey, Integer appRoleType) {
		Session session  = accountService.loginById(accountId, appKey, appRoleType);
        return session;
	}

	@Override
	public Account inquireAccountDetail(Long accountId) {
		AccountEntity accountEntity = accountService.inquireAccountDetail(accountId);
		
		Account account = accountEntity.toAccount();
		return account;
	}

	@Override
	public Account inquireAccountDetailBySessionUuid(String sessionUuid) {
		AccountEntity accountEntity = accountService.inquireAccountDetail(sessionUuid);
		
		Account account = accountEntity.toAccount();
		return account;
	}

	@Override
	public Session changePassword(String sessionUuid, String oldPassword, String newPassword) {
		return accountService.updatePassword(sessionUuid, oldPassword, newPassword);
	}

	@Override
	public void logout(String sessionUuid) {
		// TODO [wangjs] 登出暂时不删除session，方便测试。
//		accountService.logout(sessionUuid);
	}

	@Override
	public List<AccountAlias> inquireAccountAliasList(Long accountId) {
		List<AccountNameEntity> accountNameList = accountService.inquireAccountNameList(accountId);
		
		ArrayList<AccountAlias> output = new ArrayList<AccountAlias>();
		for (AccountNameEntity each : accountNameList) {
			output.add(each.toAccountAlias());
		}
		
		return output;
	}

	@Override
	public void deleteAccountAlias(Long accountId, List<String> accountNameList) {
		accountService.deleteAccountName(accountId, accountNameList);
	}

	@Override
	public void updateAccountAlias(AccountAlias accountAlias) {
		accountService.updateAccountName(accountAlias);
	}
}
